﻿/// <summary>
/// Developer: CHOOI KAH WAI
/// Last Modified: 11/10/2009
/// </summary>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace ARSS.Components
{
    /// <summary>
    /// This camera plugin will display dummy image instead of camera video
    /// </summary>
    public class CamCom_Fake : CamCom
    {
        #region CamCom Implementations

        #region Delegates / Events

        public override event Coordinator.CameraUpdateHandler CameraUpdateEvent;

        #endregion

        #region Functions

        public override string[] GetCamNames()
        {
            string[] ss = new string[1];
            ss[0] = "Fake_Image";
            return ss;
        }

        public override void Init(int screenWidth, int screenHeight, int frameRate)
        {
            // Load a dummy image
            Bitmap bmp = new Bitmap(Directory.GetCurrentDirectory() + "\\data\\fakeImg.bmp");
            this.camVideoInfo.Height = bmp.Height;
            this.camVideoInfo.VideoPixelFormat = bmp.PixelFormat;
            this.camVideoInfo.Width = bmp.Width;
            this.camVideoInfo.Bits = 24;
            this.camVideoInfo.Stride = bmp.Width * this.camVideoInfo.Bits / 8;

            BitmapData raw = bmp.LockBits(
                                   new Rectangle(0, 0, (int)bmp.Width, (int)bmp.Height),
                                   ImageLockMode.ReadOnly,
                                   bmp.PixelFormat
                               );

            this.vidBuffer = new byte[this.camVideoInfo.Stride * this.camVideoInfo.Height];

            //Copy the image into the byte[]
            System.Runtime.InteropServices.Marshal.Copy(raw.Scan0, this.vidBuffer, 0, this.vidBuffer.Length);

            bmp.UnlockBits(raw);
            bmp.Dispose();
        }

        public override void SetSelectedIndex(int _sel)
        {
            this.selectedIndex = _sel;
        }

        public override void StartCapture()
        {
            this.fakeTimer.Start();
        }

        public override void StopCapture()
        {
            this.fakeTimer.Stop();
            // Release memory
            this.vidBuffer = null;
        }

        #endregion

        #endregion

        #region Variables

        /// <summary>
        /// Video buffer
        /// </summary>
        private byte[] vidBuffer;

        /// <summary>
        /// Timer to update scene
        /// </summary>
        private Timer fakeTimer;

        #endregion

        #region Functions

        /// <summary>
        /// Constructor
        /// </summary>
        public CamCom_Fake()
        {
            this.fakeTimer = new Timer(33.33);
            this.fakeTimer.Elapsed += new ElapsedEventHandler(fakeTimer_Elapsed);
        }

        /// <summary>
        /// Timer elapsed event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void fakeTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (CameraUpdateEvent != null)
                CameraUpdateEvent(this.vidBuffer);
        }

        #endregion
    }
}
